﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace TerrariaSVCv2 {
    public class ExeWrapper {

        #region "Setter/Getters and Variables"
        Process svrProcess;
        ProcessStartInfo psi;
        string SVCLOGPATH = "";
        string SVCERRPATH = "";

        ProcessStartInfo getPSI {
            get {
                this.psi = new ProcessStartInfo();
                this.psi.Arguments = string.Format("-config {0}", "serverconfig.txt"); //static for now.. dynamic later
                this.psi.CreateNoWindow = true;
                this.psi.UseShellExecute = false;
                this.psi.WindowStyle = ProcessWindowStyle.Hidden;
                this.psi.RedirectStandardOutput = true;
                this.psi.RedirectStandardInput = true;
                this.psi.RedirectStandardError = true;
                this.psi.FileName = "TerrariaServer.exe";

#warning "TerrariaServer.exe is hard coded for location but it might be okay..."
                if (File.Exists(@"C:\Program Files (x86)\Steam\SteamApps\common\Terraria\TerrariaServer.exe")) {
                    this.psi.WorkingDirectory = @"C:\Program Files (x86)\Steam\SteamApps\common\Terraria\";
                } else if (File.Exists(@"C:\Program Files\Steam\SteamApps\common\Terraria\TerrariaServer.exe")) {
                    this.psi.WorkingDirectory = @"C:\Program Files\Steam\SteamApps\common\Terraria\";
                } else {
                    //not ready for this yet!
#warning "This needs to get updated"
                    //this.RequestSvcStop = true;
                }

                //set the log paths while we're here
                this.SVCERRPATH = string.Format(@"{0}\Service.err", this.psi.WorkingDirectory);
                this.SVCLOGPATH = string.Format(@"{0}\Service.log", this.psi.WorkingDirectory);

                return this.psi;
            }
        }
        #endregion

        public void StartProcess() {
            svrProcess = new Process();
            svrProcess.StartInfo = this.getPSI;

            svrProcess.OutputDataReceived += new DataReceivedEventHandler(svrProcess_OutputDataReceived);
            svrProcess.ErrorDataReceived += new DataReceivedEventHandler(svrProcess_ErrorDataReceived);

#warning "Check into auto save thread."

            svrProcess.Start();
            svrProcess.BeginOutputReadLine();
        }

        void svrProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
            if (!String.IsNullOrEmpty(e.Data)) {
                string message = string.Format("{0} -- {1}", DateTime.Now, e.Data);

                try {                    
                    using (StreamWriter sw = new StreamWriter(this.SVCERRPATH, true)) {
                        sw.WriteLine(message);
                    }
                } catch (Exception) { }
            }
        }

        void svrProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) {
            //for now we'll just write EVERYTHING to a file and try to send to splunk
            if (!String.IsNullOrEmpty(e.Data)) {
                string message = string.Format("{0} -- {1}", DateTime.Now, e.Data);

                try {
                    using (StreamWriter sw = new StreamWriter(this.SVCLOGPATH, true)) {
                        sw.WriteLine(message);
                    }
                } catch (Exception) { }

                try {
#warning "Remove static ip/port settings"
                    SplunkWrapper.SplunkArgs splunkArgs = new SplunkWrapper.SplunkArgs();
                    splunkArgs.HostIP = "192.168.1.109";
                    splunkArgs.Port = 16948;
                    splunkArgs.Message = message;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(SplunkWrapper.SendToSplunk), splunkArgs);

                } catch (Exception) { }
            }
        }


    }
}
